package org.dashboard.client.widget.calendar;

import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.Node;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.Element;
import com.google.gwt.user.client.Timer;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.rpc.ServiceDefTarget;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.SimplePanel;
import com.google.gwt.user.client.ui.Widget;
import org.dashboard.shared.calendar.CalendarEvent;
import org.dashboard.shared.calendar.CalendarInfo;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Simon Vogensen, Lakeside
 * @version $Revision$
 */
public class CalendarWidget extends SimplePanel {

    interface Binder extends UiBinder<Widget, CalendarWidget> {
    }

    private static final Binder binder = GWT.create(Binder.class);

    public CalendarWidget(String width, String height, int refreshInterval) {
        super();

        setWidget(binder.createAndBindUi(this));

        final Node node = getElement().getChild(0);
        DOM.setStyleAttribute((Element) node, "width", width);
        DOM.setStyleAttribute((Element) node, "height", height);

        final CalendarServiceAsync instance = (CalendarServiceAsync) GWT.create(CalendarService.class);
        String url = GWT.getModuleBaseURL() + "calendar";
        ((ServiceDefTarget) instance).setServiceEntryPoint(url);

        final AsyncCallback<ArrayList<CalendarInfo>> getWeekAsyncCallback = new AsyncCallback<ArrayList<CalendarInfo>>() {
            public void onFailure(Throwable caught) {
//                Window.alert(caught.getMessage());
            }

            public void onSuccess(ArrayList<CalendarInfo> result) {
                Node rootNode = node.getChild(0).getChild(1);
                ((Element) rootNode).setInnerHTML("" +
                        "                <div style=\"margin:5px;width:240px;font-size:40px;font-weight:bold;\">" + result.get(0).getDayName() + "</div>\n" +
                        "                <div style=\"margin:5px;width:240px;font-size:40px;font-weight:bold;\">" + result.get(1).getDayName() + "</div>\n" +
                        "                <div style=\"margin:5px;width:240px;font-size:40px;font-weight:bold;\">" + result.get(2).getDayName() + "</div>\n" +
                        "                <div style=\"margin:5px;width:240px;font-size:40px;font-weight:bold;\">" + result.get(3).getDayName() + "</div>\n" +
                        "                <div style=\"margin:5px;width:240px;font-size:40px;font-weight:bold;\">" + result.get(4).getDayName() + "</div>");

                for (int j = 0; j < 5; j++) {
                    Node dayNode = rootNode.getChild(2 * j + 1);
                    List<CalendarEvent> eventList = result.get(j).getEvents();
                    for (CalendarEvent calendarEvent : eventList) {
                        dayNode.appendChild(
                                new HTML("<div style=\"margin-left:15px;width:240px;font-size:35px;font-weight:bold;\">" + calendarEvent.getText() + "</div>").getElement());
                    }
                    dayNode.appendChild(
                            new HTML("<div style=\"margin:10px;\"></div>").getElement());
                }
            }
        };

        final Timer timer = new Timer() {
            @Override
            public void run() {
                instance.getWeek(getWeekAsyncCallback);
            }
        };
        timer.scheduleRepeating(refreshInterval);

        instance.getWeek(getWeekAsyncCallback);
    }
}
